package com.kiwi.util;

import com.netease.lottery.base.util.EncryptUtil;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.net.URLDecoder;
public class APIEncryptSet {

    private final static byte[] ecodeStr =
            { (byte) 0xef, 0x2e, (byte) 0xcc, (byte) 0xdc, (byte) 0x9b, 0x3b, (byte) 0xf7, 0x2a, 0x68, (byte) 0xad,
                    (byte) 0xeb,
                    0x72, (byte) 0xe3, 0x78, 0x2f, 0x5f, 0x7, 0x77, (byte) 0xd5, (byte) 0xc1, 0x1d, 0x40, 0x36,
                    (byte) 0xb4 };

    public static String deCode(String encodeData) throws UnsupportedEncodingException
    {

        String stamp=null;
        String data=null;
        if(encodeData.split("&")[0].contains("stamp")) {
            stamp=encodeData.split("&")[0];
            stamp=stamp.substring(stamp.indexOf("=")+1);
            data=encodeData.split("&")[1];
            data=data.substring(data.indexOf("=")+1);
        }else
        {
            data=encodeData.split("&")[0];
            data=data.substring(data.indexOf("=")+1);
            stamp=encodeData.split("&")[1];
            stamp=stamp.substring(stamp.indexOf("=")+1);
        }
        data=URLDecoder.decode(data,"utf-8");
        System.out.println("入参:" + data+" "+stamp);
        byte[] miyao = EncryptUtil.genCroptyKey(ecodeStr, stamp);
        byte[] base = EncryptUtil.getFromBASE64(data);
        byte[] decodeBytes = EncryptUtil.decryptMode(miyao, base);
        String decodeStr = new String(decodeBytes, "utf-8");
        System.out.println("base64解密后的字参数:" + decodeStr);
        return  decodeStr;
    }

    public static String enCode(String params) throws UnsupportedEncodingException
    {
        String stamp=String.valueOf(System.currentTimeMillis());
        byte[] miyao = EncryptUtil.genCroptyKey(ecodeStr, stamp);
        byte[] encoded = EncryptUtil.encryptMode(miyao, params.getBytes());
        String baseStr = EncryptUtil.getBASE64(encoded);
        System.out.println("base64加密后的参数:" + "data=" + baseStr + "&stamp=" + stamp);
        String result="data=" + baseStr + "&stamp=" + stamp;
        return result;
    }

    public static void main(String[] args) throws UnsupportedEncodingException
    {
        deCode("\n" +
               "data=veeFfXfliyeSokTgdZg3NqyibPPzOuwdFfZyUmvnTqe7%2BwFEUu7CoqWvwSkasO7WjIk4psJw%0D%0AtQ12sTpmCS3fGxFobIwZGBw0Yuv3wn5eHdM5nSi6GqR7rBwQyZ%2FppwzoAaiLCGPjHrOGJonr%0D%0AITBbTLvq835JwWb2s4cpIWZWFPL0wl71vnga324P2GqnROY%2Bc7YXYjX8pcHopiH4p0CiB8vD%0D%0AqgHJWS7iyF1IUlyvM0WGjLEyMQj8sooRlN0cUySWNLH3EPy6CxQW5MOi1z1mg72Ed8ibehVJ%0D%0APEmoS7BmBgE%3D&stamp=zleizmeainxslwhhjws");
        enCode("sessionId=897b10daeb5356e51c41cd5084331dca&accountId=67912396687");
    }
}
